package com.xgkj.diyiketang.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xgkj.diyiketang.activity.data.CommentAdapter;
import com.xgkj.diyiketang.data.CommentInfo;
import com.xgkj.diyiketang.data.PhotoInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.CommentListTask;
import com.xgkj.diyiketang.task.ZanzhaopianTask;
import com.xgkj.diyiketang.task.ZhaopianDetailTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.weidget.MyCustomListView;
import com.xgkj.diyiketang.weidget.MyScrollView;
import com.xgkj.diyiketang.R;

import java.util.List;

public class ZhaopianCommentActivity extends BaseActivity implements MyScrollView.OnScrollListener{
    private Context mContext;
    private ImageView bigImage;
    private MyCustomListView mListView;
    private CommentAdapter mAdapter;
    private PhotoInfo mPhotoInfo;
    private int pageNo = 1;
    private int size = 10;
    private int commenListTaskId = -1;
    private MyScrollView scrollView;
    private TextView jubaoText;
    private int detailTaskId = -1;
    private TextView zanView;
    private TextView pinglunText;
    private int zanTaskId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhaopian_comment);
        mContext = this;
        initView();
    }

    private void initView() {
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        mPhotoInfo = (PhotoInfo) intent.getSerializableExtra("photoInfo");
        if (mPhotoInfo == null){
            finish();
            return;
        }
        zanView = (TextView) findViewById(R.id.zan_layout);
        if (!mPhotoInfo.getIsZan()) {
            zanView.setText("已赞");
            zanView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.zan_hong, 0, 0, 0);
        }
        zanView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mPhotoInfo.getIsZan()){
                    return;
                }
                doZan();
            }
        });
        pinglunText = (TextView) findViewById(R.id.gocomment);
        pinglunText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentc = new Intent(mContext,PublishCommentActivity.class);
                intentc.putExtra("VideoId",mPhotoInfo.getImageId());
                intentc.putExtra("type",2);
                startActivity(intentc);
            }
        });
        bigImage = (ImageView) findViewById(R.id.image_detail);
        ImageCache.getInstance(mContext).loadBitmapToView(bigImage,mPhotoInfo.getPhotoURL(),null);
        mListView = (MyCustomListView) findViewById(R.id.comment_list);
        mAdapter = new CommentAdapter(mContext,mPhotoInfo.getImageId(),2);
        mListView.setAdapter(mAdapter);
        scrollView = (MyScrollView) findViewById(R.id.scrollView);
        scrollView.setOnScrollListener(this);
        jubaoText = (TextView) findViewById(R.id.jubao);
        jubaoText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent jubao_intent = new Intent(mContext,JuBaoActivity.class);
                jubao_intent.putExtra("photourl",mPhotoInfo.getPhotoURL());
                jubao_intent.putExtra("type",1);
                startActivity(jubao_intent);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        pageNo =1;
        getCommentList();
    }

    public void getCommentList() {
        if (pageNo == 0){
            mListView.setNoNextPage();
            return;
        }
        CommentListTask task = new CommentListTask(size,pageNo,mPhotoInfo.getImageId());
        task.addListener(mListener);
        commenListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void getImageDetail() {
        ZhaopianDetailTask task = new ZhaopianDetailTask(mPhotoInfo.getImageId());
        task.addListener(mListener);
        detailTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void doZan(){
        ZanzhaopianTask task = new ZanzhaopianTask(mPhotoInfo.getPhotoURL(),"0");
        task.addListener(mListener);
        zanTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private ShowToastTaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == commenListTaskId){
                if (pageNo ==1){
                    mAdapter.clearData();
                }
                if (data != null){
                    List<CommentInfo> list = (List<CommentInfo>) data;
                    mAdapter.updateData(list);
                }
                pageNo = result;
                getImageDetail();
            }else if (taskId == detailTaskId){
                if (data != null){
                    mPhotoInfo = (PhotoInfo) data;

                    if (!mPhotoInfo.getIsZan()) {
                        zanView.setText("已赞");
                        zanView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.zan_hong,0,0,0);
//                        zanView.setImageResource(R.drawable.zan_hong);
                    }else {
//                        zanView.setImageResource(R.drawable.zan_white_bg);
                        zanView.setText("点赞");
                        zanView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.zan_hui,0,0,0);
                    }

//                    String s = String.format(getString(R.string.zancomment), mPhotoInfo.getZanNum(), mPhotoInfo.getCommentNum());
//                    toComView.setText(s);
                }
            }else if (taskId == zanTaskId){
                mPhotoInfo.setIsZan(false);
                zanView.setText("已赞");
                zanView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.zan_hong,0,0,0);
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }
    };

    @Override
    public void onScroll() {
        getCommentList();
    }
}
